//
//  FriendVisitorVC.m
//  SelfDriving
//
//  Created by apple on 16/2/5.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "FriendVisitorVC.h"
#import "MemberRoamingCell.h"
#import "BusinessService+WD.h"
#import "FriendDetailsMainVC.h"
@interface FriendVisitorVC ()<BaseCollectionDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet BaseCollection *collection;

@end

@implementation FriendVisitorVC{
    NSMutableArray *data;
    UIView *notview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    data = [[NSMutableArray alloc]init];
    _collection.bdelegate = self;
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initData:) name:@"refreshData" object:nil];
//    self.collection.scrollEnabled = NO;
}
-(void)initData:(int)index{
    NSMutableDictionary *dic = [UserDefaults readUserDefaultObjectValueForKey:@"userLocation"];
    [dic setValue:@"0" forKey:@"type"];
    [dic setObject:[NSNumber numberWithInt:index] forKey:@"page"];
    [dic setObject:_uid forKey:@"fid"];
    [[BusinessService shareInstance]getdynamic_guest:dic success:^(id result) {
        
        if (result) {
            @try {
                
                data = [_collection result:result[@"list"] olddata:data index:index];
                
                
                if (data.count == 0) {
                    
                    [self notdongtai];
                    
                }else{
                    
                    [notview removeFromSuperview];
                }
                
                [_collection reloadData];

                
            } @catch (NSException *exception) {
                
            }
           
        
        }
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
}
#pragma  mark 无动态数据时
-(void)notdongtai{
    
    notview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCRE_WIDTH, _collection.frame.size.height)];
    [_collection addSubview:notview];
    
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(SCRE_WIDTH/2-40, 44, 80, 80)];
    img.image = [UIImage imageNamed:@"skhead"];
    [notview addSubview:img];
    
    
    UILabel *hi = [[UILabel alloc]initWithFrame:CGRectMake(0, 130, SCRE_WIDTH, 30)];
    hi.textAlignment = NSTextAlignmentCenter;
    hi.text = @"hi,嘿";
    [notview addSubview:hi];
    
    UILabel *fj = [[UILabel alloc]initWithFrame:CGRectMake(0, 160, SCRE_WIDTH, 30)];
    fj.textAlignment = NSTextAlignmentCenter;
    fj.text = @"没人阅读过你的动态";
    [notview addSubview:fj];
    
    UILabel *sk = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, SCRE_WIDTH, 100)];
    sk.textAlignment = NSTextAlignmentCenter;
    sk.text = @"阅读过动态的人只记录期间会员期间的用户";
    sk.font = [UIFont systemFontOfSize:14];
    sk.textColor = [UIColor grayColor];
    sk.numberOfLines = 0;
    [notview addSubview:sk];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)sectio{
    return data.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MemberRoamingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MemberRoamingCell" forIndexPath:indexPath];
    LoginResponseModel *user = [data objectAtIndex:indexPath.row];
    cell.name_label.text = user.nickname;
    cell.name_label.textColor = [UIColor whiteColor];
    cell.vip_img.hidden = YES;
    [ValidateHelper setImage:user.headsmall img:cell.head_img istoux:NO];
//    NSString *age = [ValidateHelper timeToAge:user.age];
//    NSString *dis = [ValidateHelper DistanceFormat:user.distance];
//    NSString *time = [ValidateHelper timeStringWith:[user.viewtime doubleValue] multiLine:NO];
//    NSString *sign = [NSString stringWithFormat:@"%@岁 %@ %@",age,dis,time];
//    cell.sign_lable.text = sign;
    cell.sign_lable.hidden = YES;
    
    cell.head_img.layer.cornerRadius = cell.frame.size.width/2;
    cell.head_img.clipsToBounds = YES;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCRE_WIDTH-30)/6, (SCRE_WIDTH-30)/6);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    LoginResponseModel *model = [data objectAtIndex:indexPath.row];
    
    DTModel *mode = [[DTModel alloc]init];
    mode.uid = model.uid;
    FriendDetailsMainVC *VC = (FriendDetailsMainVC *)(self.mainVC);
    [VC XhwTogerenActions:mode];
    //之前是通过发通知的方式来触发事件，因为self.navigationController中已经有了几个已经注册了“togerenActions”的FriendDetailViewController,而且在通知togerenActions调用的方法中是写的self.navigationViewController push一个FriendDetailViewController，也就是说有多个接受通知的地方，所以就push了多次。解决方法是不用通知的方式来触发事件，而是通过对象FriendDetailsMainVC *VC调用方法的方式来触发，当然也可以用delegate来触发(xhw11月8号改)
//    [[NSNotificationCenter defaultCenter ]postNotificationName:@"togerenActions" object:mode];


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)backActino:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat y = scrollView.contentOffset.y;
    
    NSLog(@"相册移动==：xw --%f",y);
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"xwScroll" object:nil userInfo:@{@"key":[NSString stringWithFormat:@"%f",y]}];
    
}

@end
